import os
from google.appengine.ext.webapp import template

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext import db

class userdata(db.Expando):
    name = db.StringProperty()
    user = db.UserProperty()
    role = db.StringProperty()

def get_user_data(user):

    if(user == None):
        user_data = userdata()
        user_data.user = None
        user_data.role = "Read-only"
        return user_data
    else:
        query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", user)
        user_data = query.get()
        if(user_data == None):
            user_data = userdata()
            user_data.user = user
            #user_data.role = "User"
            user_data.role = "Read-only"
        return user_data

def get_login_info(self):
    user = users.get_current_user()
    user_data = get_user_data(user)
    if user:
        log_url = users.create_logout_url(self.request.uri)
    else:
        log_url = users.create_login_url(self.request.uri)

    return {'user': user, 'user_data': user_data, 'log_url': log_url}
    
class listUserdata(webapp.RequestHandler):
    def get(self):

        if(users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:
            
            user_data_list = userdata.all()
            template_values = { 'user_data_list': user_data_list }

            path = os.path.join(os.path.dirname(__file__), 'templates/listUserdata.html')
            self.response.out.write(template.render(path, template_values))

class addUserdata(webapp.RequestHandler):
    def get(self):

        if( users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:
            template_values = { }

            path = os.path.join(os.path.dirname(__file__), 'templates/addUserdata.html')
            self.response.out.write(template.render(path, template_values))

    def post(self):

        if(users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:

            query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", users.User(self.request.get('username') + '@gmail.com'))
            user_data = query.get()
            if (user_data != None):
                self.response.out.write('Error: User data exists')
            else:
                user_data = userdata()
                user_data.name = self.request.get('name')
                user_data.user = users.User(self.request.get('username') + '@gmail.com')
                user_data.role = self.request.get('role')
    
                user_data.put()
    
                self.redirect('/listUserdata')

class editUserdata(webapp.RequestHandler):
    def get(self, username):
        if( users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:
            
            query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", users.User(username + '@gmail.com'))
            user_data = query.get()
            if (user_data == None):
                self.response.out.write('Error: User not found')
            else:

                template_values = {'user_data': user_data }
    
                path = os.path.join(os.path.dirname(__file__), 'templates/editUserdata.html')
                self.response.out.write(template.render(path, template_values))

    def post(self, username):

        if(users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:

            query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", users.User(username + '@gmail.com'))
            user_data = query.get()
            if (user_data == None):
                self.response.out.write('Error: User not found')
            else:
                user_data.name = self.request.get('name')
                user_data.user = users.User(self.request.get('username') + '@gmail.com')
                user_data.role = self.request.get('role')
    
                user_data.put()
    
                self.redirect('/listUserdata')

class deleteUserdata(webapp.RequestHandler):
    def get(self, username):

        if( users.is_current_user_admin() != True):
            self.response.out.write('Access denied: Administrators only')
        else:
            
            query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", users.User(username + '@gmail.com'))
            user_data = query.get()
            if (user_data == None):
                self.response.out.write('Error: User data not found')
            else:

                template_values = {'user_data': user_data }
    
                path = os.path.join(os.path.dirname(__file__), 'templates/confirmDeleteUserdata.html')
                self.response.out.write(template.render(path, template_values))

    def post(self, username):

        if( users.is_current_user_admin() != True):
            self.response.out.write('Acess denied: Administrators only')
        else:

            query = db.GqlQuery("SELECT * FROM userdata WHERE user = :1", users.User(username + '@gmail.com'))
            user_data = query.get()
            if(user_data == None):
                self.response.out.write('Error: User data not found')
            else:
                user_data.delete()

                self.redirect('/listUserdata')
